package koscom.corebase.nam;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
//import redis.clients.jedis.Jedis;

/**
 * @author Nam
 *
 */
public class RQReader implements Runnable {
	public static boolean isSet;
	public static boolean isRun;
	public static boolean isClear = false;
	public static String hostString = "192.168.172.137"; //"172.16.52.134";
	public static List<String> bindKeys;
	
	
	public static ConnectionFactory factory = null;
    public static Connection connection = null;
    public static Channel channel = null;
    public static String exchangeName = "nam_sise";
    public static String queueName = null;
    public QueueingConsumer consumer;
    
	public RQReader(String exchangeName)
	{
        factory = new ConnectionFactory();
        factory.setHost(hostString);
        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            queueName = new String("namQ");
            channel.exchangeDeclare(exchangeName, "topic");
            
        } catch (IOException ex) {
            Logger.getLogger(RQReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        isSet = false;
        isRun = false;
        bindKeys = new ArrayList<String>();
	}
	
	public static void unbindAll()
	{
		System.out.println("unbind, clear");
		try{
			for(String topic : bindKeys){
				System.out.println("unbind-for :"+topic);
				channel.queueUnbind(queueName, exchangeName, topic);
			}
			bindKeys.removeAll(bindKeys);
			//if(channel.isOpen()) channel.close();
			
			//channel = connection.createChannel();
		}
		catch(Exception e){ System.out.println("unbind ex");}
	}

	@Override
	public void run() {
		while(true)
		{
			if(isSet)
			{
				System.out.println("bindQ");
				try{
				//channel = connection.createChannel();
				//channel.exchangeDeclare(exchangeName, "topic");
				channel.queueDelete(queueName);
				channel.queueDeclare(queueName, false, false, false, null);
				consumer = new QueueingConsumer(channel);
				channel.basicConsume(queueName, true, consumer);
				
				for (String topic : bindKeys) {
					System.out.println("bind for : ["+topic+"]");
	                channel.queueBind(queueName, exchangeName, topic);
	            }
				}catch(Exception ex){ Logger.getLogger(RQReader.class.getName()).log(Level.SEVERE, null, ex); System.out.println("bind err?"); }
				isSet = false;
				isRun = true;
			}
			while(isRun)
			{
				if(isSet==true) break;
				//System.out.println("run");
				try{
		            QueueingConsumer.Delivery delivery = consumer.nextDelivery(100); //timeout - timeout in millisecond
		            if(delivery==null) continue;
		            //System.out.println("not null");
		            String message = new String(delivery.getBody());
		            
		            String topic = delivery.getEnvelope().getRoutingKey();
		            if(topic.indexOf("A3011")!=-1)
		            {//A3011
		            	A2007.parsing(1, message);
		            }
		            else if(topic.indexOf("B6011")!=-1)
		            {//B6011
		            	A2007.parsing(2, message);
		            }
		            System.out.println("[Realtime data] = ["+ message +"]");
		            
		            Thread.sleep(1000);
		        } catch (Exception ex) {
		            Logger.getLogger(RQReader.class.getName()).log(Level.SEVERE, null, ex);
		            try{ int i = System.in.read(); System.out.println(i);} catch(Exception e){}
		        }
			}
		}
	}
}